package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.constant.MessageConstant;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.pojo.Setmeal;
import com.itcast.service.CheckItemService;
import com.itcast.entity.Result;
import com.itcast.pojo.CheckItem;
import com.itcast.service.SetmealService;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 检查项管理
 */


@RestController //RestController  = @RequestBody+@Controller
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference  //dubbo从网络服务获取
    private CheckItemService checkItemService;

    @Reference
    private SetmealService setmealService;

//    private List<Setmeal> list= setmealService.findAll();


    /**
     * 添加
     * @param checkItem
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @RequestMapping("/add")
   //前端发送的ajax数据是json形式，所以必须使用@RequestBody来接收
    public Result add(@RequestBody CheckItem checkItem){
//        在实际开发中使用try。。。catch来判断是否操作（更新，删除，修改）成功   查询则直接返回对应的对象
        try {
            Integer id = checkItemService.add(checkItem);

        } catch (Exception e) {
            e.printStackTrace();
//            失败或报错返回失败消息和false状态方便前端处理
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
//        成功返回状态和消息
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 分页查找
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkItemService.findPage(queryPageBean);

        return pageResult;
    }

    /**
     * 删除
     * @param map
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/delrow")
    public Result delRow(@RequestBody Map<String,Integer> map){
        try{
            Integer id = map.get("id");
            checkItemService.delRow(id);
            setmealService.generaStaticSetmealDetailHtml(setmealService.findAll());
        }catch (Exception e ){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 回显数据
     */
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/editByid")
    public Result findByid(int id){
        try {
            CheckItem checkItemList = checkItemService.findByid(id);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS,checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }

    }

    /**
     * 修改信息
     * @param checkItem
     * @return 返回Result
     */
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
        @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
            List<Integer> setmeaIds = checkItemService.getSetmeaIds(checkItem.getId());
            List<Setmeal> setmeals = checkItemService.getSetmeals(setmeaIds);
            if (setmeals!=null&&setmeals.size()>0) {
                setmealService.generaStaticSetmealDetailHtml(setmeals);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

}
